package com.ys.controller.api.base;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ys.config.MenuId;
import com.ys.config.MyConfig;
import com.ys.entity.base.TbUserAttendance;
import com.ys.service.sys.CommonApiService;
import com.ys.util.baidu.ApiHttpClientUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

/**
 * 员工考勤信息表
 */
@Controller
@RequestMapping("/api/inter/tbUserAttendance")
public class ApiInterTbUserAttendanceController {
    private static final Logger logger = LoggerFactory.getLogger(ApiInterTbUserAttendanceController.class);

    @Autowired
    private CommonApiService<TbUserAttendance> commonApiService;

    @Autowired
    private ApiHttpClientUtils apiHttpUtils;

    private static String URLAPI2_0 = MyConfig.getUrlApi1_5();



    /**
     * 设置查询的cond的sql设置
     * @param cond
     * @return
     */
    public TbUserAttendance setSeachSql(TbUserAttendance cond){
        cond.addLeftJoin("TS_UserInfo U ON U.UserId = A.UserId")//用户信息表
                .addField("(U.UserId + '/' + U.UserName) AS userName");//工号/姓名
        //排序
        cond.setOrderby("A.attendanceMonth DESC,A.UserId ASC");
        cond = commonApiService.dataAuth(MenuId.TB_UserAttendance, cond);
        return cond;
    }


    /**
     * 查询
     */
    @RequiresPermissions("tbUserAttendance:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(@ModelAttribute TbUserAttendance cond){
        try {
            cond = setSeachSql(cond);
            LayuiTableVoJA tableVoJA = commonApiService.searchLay(cond);
            return tableVoJA;
        }catch (Exception e){
            logger.error("查询出错：",e.getMessage(),e);
            LayuiTableVoJA tableVoJA = new LayuiTableVoJA();
            tableVoJA.setCode(1);
            tableVoJA.setMsg("查询出错，请稍后重试！");
            return tableVoJA;
        }
    }




    /**
     * 从钉钉获取考勤报表数据
     */
    @RequestMapping("/getcolumnvalByDingDing")
    @ResponseBody
    public R getcolumnvalByDingDing(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            String sentUrl = URLAPI2_0;
            //Map<String, Object> sendParams = new HashMap<>();
            sentUrl = sentUrl + "/api/outer/dingDingApi/getcolumnvalByDingDing";
            //sendParams.put("countMonth",countMonth);//统计的考勤日期
            apiHttpUtils.sendHttpPost(params,sentUrl);
            return R.ok();
        } catch (Exception e) {
            logger.error("出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


    /**
     * 根据手机号刷新用户钉钉id
     */
    @RequestMapping("/getDingDingUserIdByPhone")
    @ResponseBody
    public R getDingDingUserIdByPhone(HttpServletRequest request, String telephone, String userId){
        try {
            String sentUrl = URLAPI2_0;
            Map<String, Object> sendParams = new HashMap<>();
            sentUrl = sentUrl + "/api/outer/dingDingApi/getDingDingUserIdByPhone";
            sendParams.put("telephone",telephone);//联系电话
            sendParams.put("userId",userId);//用户id
            JSONObject chatBack = apiHttpUtils.sendHttpPost(sendParams,sentUrl);
            String dingdingId = "";
            if(chatBack!=null){
                if(Integer.parseInt(chatBack.get("code").toString())==1){
                    dingdingId = chatBack.get("dingdingId").toString();
                }else {
                    return R.error("获取出错，请核对手机号！");
                }
            }else {
                return R.error("获取出错，请核对手机号！");
            }
            return R.ok(dingdingId);
        } catch (Exception e) {
            logger.error("出错：",e.getMessage(),e);
            return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
        }
    }


}